package region

import (
	"encoding/json"
	"fmt"
	"hft/Database/admin"
	"hft/Http/Models/request"
	services "hft/Http/Services/region"
	utils "hft/Http/Utils"
	"hft/global"
	"hft/tools"
	"io/ioutil"
	"net/http"

	"github.com/gin-gonic/gin"
)

type Region struct{}

func (r *Region) GetRegion(c *gin.Context) {

	var responseData admin.Region

	client := &http.Client{}

	url := fmt.Sprintf("%s/config/district?key=%s&subdistrict=%s&page=%s&offset=%s", global.OC_CONFIG.Amap.Url, global.OC_CONFIG.Amap.Key, "3", "1", "50")

	request, err := http.NewRequest(http.MethodGet, url, nil)

	if err != nil {
		panic(err)
	}

	response, _ := client.Do(request)

	respData, err := ioutil.ReadAll(response.Body)

	if err != nil {
		return
	}

	json.Unmarshal(respData, &responseData)

	var region = responseData.Districts[0]

	services.RegionService.SaveRegion(region)

	utils.OkWithData(&responseData, c)
}

func (r *Region) GetRegionList(c *gin.Context) {

	regions, _ := services.RegionService.GetRegionList()

	utils.OkWithData(regions, c)
}

func (*Region) GetRegionByAccount(c *gin.Context) {

	var requestRegion *request.RegionInfoList

	c.ShouldBind(&requestRegion)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	list, err := services.RegionService.GetRegionByAccount(requestRegion, claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(list, c)
}

func (*Region) SetHot(c *gin.Context) {

	var data *request.SetHot

	c.ShouldBindJSON(&data)

	err := services.RegionService.SetHot(data.IsHot, data.City)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)

}

func (*Region) SetDefault(c *gin.Context) {

	var data *request.SetDefault

	c.ShouldBindJSON(&data)

	err := services.RegionService.SetDefault(data.IsDefault, data.City)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)

}

func (*Region) Delete(c *gin.Context) {
	var ids *request.IDS

	c.ShouldBindJSON(&ids)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	err = services.RegionService.Delete(ids.IDS, claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)
}

func (*Region) AddRegion(c *gin.Context) {
	var region *admin.Region
	c.ShouldBindJSON(&region)
	claims, err := tools.GetClaims(c)
	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}
	err = services.RegionService.AddRegion(region, claims.BaseClaims)
	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}
	utils.Ok(c)
}

func (*Region) EditRegion(c *gin.Context) {
	var region *admin.Region
	c.ShouldBindJSON(&region)
	claims, err := tools.GetClaims(c)
	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}
	err = services.RegionService.EditRegion(region, claims.BaseClaims)
	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}
	utils.Ok(c)
}

var RegionController = new(Region)
